package com.example.service.impl;

import com.example.mapper.MaterialMapper;
import com.example.mapper.OutMaterialsMapper;
import com.example.pojo.Emp;
import com.example.pojo.Materials;
import com.example.pojo.OutMaterials;
import com.example.pojo.PageBean;
import com.example.service.MaterialService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.List;

@Service
public class MaterialServiceImpl implements MaterialService {
    @Autowired
    private MaterialMapper materialMapper;
    @Autowired
    private OutMaterialsMapper outMaterialsMapper;

    @Override
    public PageBean page(Integer page, Integer pageSize, String materialName, int stockQuantity) {
        Long count = materialMapper.count(materialName,stockQuantity);//总数返回
        Integer start=(page-1)*pageSize;//计算起始位置
        List<Materials> empList = materialMapper.page(start, pageSize,materialName,stockQuantity);//该页记录数返回

        PageBean pageBean = new PageBean(count, empList);
        return pageBean;
    }

    @Override
    public void add(Materials materials) {
        materialMapper.add(materials);
    }

    @Override
    public void addUpdate(Materials materials) {
        materialMapper.update(materials);
    }

    @Override
    public void deletes(List<Integer> ids) {
        materialMapper.deletes(ids);
    }

    @Override
    public List<Materials> getIDs() {
        List<Materials> materials=materialMapper.selectIDs();
        return materials;
    }

    @Override
    public Materials getOne(String metrname) {

        return materialMapper.getOne(metrname);
    }

    @Override
    public void update(String name, int num) {
        int sum=num*materialMapper.getprices(name);
        String unit=materialMapper.getunit(name);
        OutMaterials outMaterials=new OutMaterials();
        outMaterials.setMname(name);
        outMaterials.setNum(num);
        outMaterials.setPrice(materialMapper.getprices(name));
        outMaterials.setTime(LocalDate.now());
        outMaterials.setTotal(sum);
        outMaterials.setNote("取料出库"+name+"件"+num+unit+"成功，花费"+sum+"元");
        outMaterialsMapper.add(outMaterials);
        materialMapper.updatese(name,num);
    }
}
